package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Receive;
import com.mju.service.IReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/receive")
@Api(tags = "回收服务")
public class ReceiveController extends BaseController {
    @Autowired
    private IReceiveService receiveService;

    // 创建回收单
    @ApiOperation("创建回收单")
    @PostMapping("/addReceiveInfo")
    public JsonResult addReceiveInfo(@RequestBody Receive receive, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StringUtils.isEmpty(token)) {
            return new JsonResult(OK, "用户未登录或用户状态异常");
        }
        Map<String, String> map = parseTokenGetUidAndUname(token);
        Integer uid = Integer.valueOf(map.get("uid"));
        String username = map.get("uname");
//        uid=1;
//        username="xxx";
        receive.setUId(uid);
        receive.setUserName(username);
        String message = receiveService.addReceiveInfo(receive) ? "创建回收单成功" : "创建回收单失败";
        return new  JsonResult(OK,message);
    }
    @ApiOperation("审核回收单")
    @PutMapping("/reviewReceiveInfo")
    public JsonResult reviewReceiveInfo(@RequestParam("rid") Integer rid, @RequestParam ("finalPrice")BigDecimal finalPrice){
        String message = receiveService.reviewReceiveInfo(rid, finalPrice) ? "审核回收单成功" : "审核回收单失败";
        return new  JsonResult(OK,message);
    }

    @SneakyThrows
    @ApiOperation("上门回收")
    @PutMapping("/faceToFaceReceive")
    public JsonResult faceToFaceReceive(@RequestParam("rid") Integer rid,
                                        @RequestParam("predictTimeStart") String predictTimeStart,
                                        @RequestParam("predictTimeEnd") String predictTimeEnd) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newPredictTimeStart = dateFormat.parse(predictTimeStart);
        Date newPredictTimeEnd = dateFormat.parse(predictTimeEnd);
        String message = receiveService.faceToFaceReceive(rid, newPredictTimeStart, newPredictTimeEnd) ? "安排上门回收成功" : "安排上门回收失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("已取货，待打款")
    @PutMapping("/waitingForPayment")
    public JsonResult waitingForPayment(@RequestParam("rid") Integer rid) {
        String message = receiveService.waitingForPayment(rid) ? "操作完成" : "操作异常";
        return new JsonResult(OK, message);
    }

    @ApiOperation("回收单完成")
    @PutMapping("/completeReceive")
    public JsonResult completeReceive(@RequestParam("rid") Integer rid){
        String message = receiveService.completeReceive(rid) ? "回收单完成" : "回收单异常";
        return new JsonResult(OK,message);
    }

    @ApiOperation("获取回收单状态")
    @GetMapping("/getStatusByRid/{rid}")
    public Integer getStatusByRid(@PathVariable("rid") Integer rid) {
        return receiveService.getStatusByRid(rid);
    }

    @ApiOperation("取消回收单")
    @PutMapping("/cancelReceive")
    public JsonResult cancelReceive(@RequestParam("rid") Integer rid){
        String message = receiveService.cancelReceive(rid) ? "取消回收单成功" : "取消回收单失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("用户确认回收")
    @PutMapping("/userConfirmsReceive")
    public JsonResult userConfirmsReceive(@RequestParam("rid") Integer rid){
        String message = receiveService.userConfirmsReceive(rid) ? "您已同意回收" : "同意回收失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("修改回收单信息")
    @PutMapping("/updateReceiveInfo")
    public JsonResult updateReceiveInfo(@RequestBody Receive receive){
        Receive data = receiveService.updateReceiveInfo(receive);
        return new JsonResult(OK,data);
    }

    @ApiOperation("修改回收单状态")
    @PutMapping("/updateStatusByRid")
    public JsonResult updateStatusByRid(@RequestParam("rid") Integer rid,@RequestParam("status")Integer status){
        String message = receiveService.updateStatusByRid(rid, status) ? "修改回收单状态成功" : "修改回收单状态失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("修改回收人员")
    @PutMapping("/updateRepoManByRid")
    public JsonResult updateRepoManByRid(@RequestParam("rid") Integer rid,@RequestParam("repoMan")String repoMan){
        String message = receiveService.updateRepoManByRid(rid, repoMan) ? "修改回收人员成功" : "修改回收人员失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("删除回收单")
    @PostMapping("/deleteReceiveInfo")
    public JsonResult deleteReceiveInfo(@RequestParam("rid") Integer rid){
        String message = receiveService.deleteReceiveInfo(rid) ? "删除回收单成功" : "删除回收单失败";
        return new JsonResult(OK,message);
    }

    @ApiOperation("上传图片")
    @PostMapping("/uploadPictures")
    public JsonResult<String> uploadPictures(HttpSession session, MultipartFile file) {
        String path = receiveService.uploadPictures(session, file);
        return new JsonResult<>(OK, path);
    }

    //无条件查询所有回收单信息
    @ApiOperation("无条件查询所有回收单信息")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<Receive>> queryAllReceiveInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<Receive> data = receiveService.queryAllReceiveInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    //条件查询所有回收单信息
    @ApiOperation("条件查询所有回收单信息，条件最多包含回收单id、用户id、模糊用户名、回收单状态")
    @PostMapping("/conQueryAllReceiveInfo")
    public JsonResult<IPage<Receive>> conQueryAllReceiveInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,@RequestBody Receive receive){
        IPage<Receive> data = receiveService.conQueryAllReceiveInfo(currentPage, pageSize,receive);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = receiveService.conQueryAllReceiveInfo((int)data.getPages(), pageSize,receive);
        }
        return new JsonResult<>(OK,data);
    }
}
